<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/*声明一个数组类型变量，可以在构造函数中赋值，也可以向视图中分配*/
	var $data = array(); 
	
    function __construct()
    {
        /*在构造方法中加载模型和类库*/
        parent::__construct();
		$this->load->model('m_user');
		$this->load->library('form_validation');
    }

	public function index()
	{
		/*调用model的方法判断用户是否登录并设置session信息*/
		if ($this->m_user->is_logged_in()) { redirect('admin'); }
	    
		/*设置表单验证规则*/
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		
		if ($this->form_validation->run()) {
			/*客户端验证成功，将表单信息保存至变量中*/
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			/*获取有关用户名的记录*/
			if ($user = $this->m_user->get_by_username($username)) 
			{	
				/*将记录的密码字段和表单中的密码字段进行比较*/
				if ($this->m_user->check_password( $password, $user['password'] )) 
				{
					/*服务器端验证成功，设置session并跳转*/
					$this->m_user->allow_pass( $user );
					redirect('admin');
				} 
				else 
				{ 	
					$this->data['login_error'] = 'Invalid username or password'; 
				}
			} 
			else 
			{
				$this->data['login_error'] = 'Username not found'; 
			}
	    }
		/*显示登录界面，传递相关信息*/	
		$this->load->view('login/v_login', $this->data);
	}
	
	
	public function register() {
		/*调用model的方法判断用户是否登录并设置session信息*/
		if ($this->m_user->is_logged_in()) { redirect('admin'); }

		/*设置注册表单的表单验证规则*/
		$this->form_validation->set_rules('fullname', 'Full Name', 'required');
		$this->form_validation->set_rules('username', 'Username', 'required|is_unique[users.username]');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_rules('cpassword', 'Password', 'required|matches[password]');
		
		if ($this->form_validation->run()) {
			/*验证成功则使用数组保存用户的注册信息*/
			$user = array(
				'username' => $this->input->post('username'),
				'email' => $this->input->post('email'),
				'fullname' => $this->input->post('fullname'),
				'password' => $this->m_user->hash_password( $this->input->post('password') )
			);
			/*保存用户的注册信息，如果注册成功，保存登录链接，如果注册失败，保存错误信息*/
			if ( $this->m_user->save($user) ) {
				$this->data['register_success'] = 'Registration successful. <a href="'.site_url('login').'">Click here to login</a>.';
			} else { $this->data['register_error'] = 'Saving data failed. Contact administrator.'; }
		}
		/*显示登录页面，传递相关验证信息*/
		$this->load->view('login/v_register', $this->data);
	}
	

	public function logout() {
		/*清空用户的session信息*/
		$this->m_user->remove_pass();
		/*保存提示信息*/
		$this->data['login_success'] = 'You have been logged out. Thank you.';
		/*加载视图，显示信息*/
		$this->load->view('login/v_login', $this->data);
	}
	
	/*未登录显示未登录信息*/
	public function noaccess() {
		/*输出错误提示*/
		$this->data['login_error'] = 'You do not have access or your login has expired.';
		/*跳转到登录界面*/
		$this->load->view('login/v_login', $this->data);
	}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */